package com.hospital.workstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hospital.common.utils.StringUtils;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hospital.workstation.domain.bo.HisNurseRowsBo;
import com.hospital.workstation.domain.vo.HisNurseRowsVo;
import com.hospital.workstation.domain.HisNurseRows;
import com.hospital.workstation.mapper.HisNurseRowsMapper;
import com.hospital.workstation.service.IHisNurseRowsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 护理记录Service业务层处理
 *
 * @author whb
 * @date 2023-03-09
 */
@RequiredArgsConstructor
@Service
public class HisNurseRowsServiceImpl implements IHisNurseRowsService {

    private final HisNurseRowsMapper baseMapper;

    /**
     * 查询护理记录
     */
    @Override
    public HisNurseRowsVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询护理记录列表
     */
    @Override
    public TableDataInfo<HisNurseRowsVo> queryPageList(HisNurseRowsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HisNurseRows> lqw = buildQueryWrapper(bo);
        Page<HisNurseRowsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询护理记录列表
     */
    @Override
    public List<HisNurseRowsVo> queryList(HisNurseRowsBo bo) {
        LambdaQueryWrapper<HisNurseRows> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HisNurseRows> buildQueryWrapper(HisNurseRowsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HisNurseRows> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMedicalRecordNo() != null, HisNurseRows::getMedicalRecordNo, bo.getMedicalRecordNo());
        lqw.eq(bo.getNurseTime() != null, HisNurseRows::getNurseTime, bo.getNurseTime());
        lqw.eq(StringUtils.isNotBlank(bo.getNurseContent()), HisNurseRows::getNurseContent, bo.getNurseContent());
        lqw.eq(StringUtils.isNotBlank(bo.getNurseManCode()), HisNurseRows::getNurseManCode, bo.getNurseManCode());
        lqw.like(StringUtils.isNotBlank(bo.getNurseManName()), HisNurseRows::getNurseManName, bo.getNurseManName());
        lqw.between(params.get("beginTime") != null && params.get("endTime") != null, HisNurseRows::getNurseTime,
            params.get("beginTime") + " 00:00:00", params.get("endTime") + " 23:59:59");
        lqw.orderByDesc(HisNurseRows::getNurseTime);
        return lqw;
    }

    /**
     * 新增护理记录
     */
    @Override
    public Boolean insertByBo(HisNurseRowsBo bo) {
        HisNurseRows add = BeanUtil.toBean(bo, HisNurseRows.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改护理记录
     */
    @Override
    public Boolean updateByBo(HisNurseRowsBo bo) {
        HisNurseRows update = BeanUtil.toBean(bo, HisNurseRows.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HisNurseRows entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除护理记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
